package com.base.fileupload;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class FileUploadServlet extends HttpServlet {
	private static int COUNT = 0;

	private static final int UPLOAD_SUCCSSS=0;    // "上传文件成功！", 
	private static final int UPLOAD_FAILURE=1;    // "上传文件失败！"), 
	private static final int UPLOAD_TYPE_ERROR=2; // "上传文件类型错误！"), 
	private static final int UPLOAD_OVERSIZE=3;   // "上传文件过大！"),
	private static final int UPLOAD_ZEROSIZE=4;   // "上传文件为空！"),
	private static final int UPLOAD_NOTFOUND=5;   // "上传文件路径错误！")

	private static final SimpleDateFormat dateFmt=new SimpleDateFormat("yyyyMMddHHmmss"); 
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		response.setContentType("text/html; charset=UTF-8");
		
		PrintWriter out=response.getWriter();
		 String rootPath = request.getParameter("rootPath");  
	
		 String param = request.getParameter("param");
		 
		 if(rootPath == null) rootPath = "";  
		    rootPath = rootPath.trim();  
		 if(rootPath.equals("")){  
			 rootPath = this.getServletContext().getRealPath("");  
		 }  
		
		 //上传操作  
		  FileItemFactory factory = new DiskFileItemFactory();  
		  ServletFileUpload upload = new ServletFileUpload(factory);  
		  upload.setHeaderEncoding("UTF-8");  
		  try{  
		      List items = upload.parseRequest(request);  
		      if(null != items){  
		          Iterator itr = items.iterator();  
		          while(itr.hasNext()){  
		              FileItem item = (FileItem)itr.next();
		              if(item.isFormField()){  
		                 continue;  
		              }else{  
		                   //以当前精确到秒的日期为上传的文件的文件名 
		                  Date now = new Date();
		                  String dateFileName = dateFmt.format(now);
		                  String path="/files";
		                  String fileName = item.getName();
		                  int position = fileName.lastIndexOf(".");     
		                  String extension = fileName.substring(position);
		                  String name = fileName.substring(0,position);
		                  File savedFile = new File(rootPath+path,dateFileName+(++COUNT)+extension);  
		                  item.write(savedFile);
		                  out.print("{status:"+this.UPLOAD_SUCCSSS+",message:'"+path+"/"+savedFile.getName()+"',title:'"+name+"'}");
		              }  
		          }  
		      }
		  }catch(Exception e){  
		      e.printStackTrace();  
		  }
	}
}
